<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);

$find = MiscUtils::getParam('find', '');
$cat = MiscUtils::getParam('cat', 0);
$from = MiscUtils::getParam('from', '');
$to = MiscUtils::getParam('to', '');

$cond_vals = new stdClass(); 
$cond_vals->c = '';
$cond_vals->v = array();
$cond_vals->c = "(t.first_name LIKE :v1 OR t.last_name LIKE :v1 OR t.account LIKE :v1 OR t.phone LIKE :v1)";
$cond_vals->v[":v1"] = "%$find%";

if ($cat == 1) {
  $cond_vals->c .= " AND t.is_actived = :v2";
  $cond_vals->v[":v2"] = 0;
} else if ($cat == 2) {
  $cond_vals->c .= " AND t.is_actived = :v2";
  $cond_vals->v[":v2"] = 1;
} else if ($cat == 3) {
  $cond_vals->c .= " AND t.is_actived = :v2";
  $cond_vals->v[":v2"] = 2;
}

if ($from != '' && $to != '') {
  $cond_vals->c .= " AND (t.reg_date >= :v3 AND t.reg_date <= :v4)";
  $cond_vals->v[":v3"] = $from;
  $cond_vals->v[":v4"] = $to;
} else if ($from != '' && $to == '') {
  $cond_vals->c .= " AND t.reg_date >= :v3";
  $cond_vals->v[":v3"] = $from;
} else if ($from == '' && $to != '') {
  $cond_vals->c .= " AND t.reg_date <= :v3";
  $cond_vals->v[":v3"] = $to;
}

$SIZE = 50;
$page = MiscUtils::getParam('p', 1);
$order = MiscUtils::getParam('o', 't.account');
$queue = MiscUtils::getParam('q', 'ASC');
$items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $cond_vals, $order, $queue, $page, $SIZE, NULL);

$members = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_member', NULL, 't.total_spent', 'ASC', NULL, NULL, NULL)->d;
$status = array();
$s1 = new stdClass();
$s1->id = 0;
$s1->name = 'Inactivated';
$s2 = new stdClass();
$s2->id = 1;
$s2->name = 'Activated';
$s3 = new stdClass();
$s3->id = 2;
$s3->name = 'Suspended';
array_push($status, $s1, $s2, $s3);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.list {
  border-left: 1px solid #666;
  border-top: 1px solid #666;
  font-size: 12px;
  width: 950px;
}
table.list th.sort {
  text-align: left;
  height: 28px;
  background-color: #222;
  padding: 0 5px; 
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  font-weight: bold;
  vertical-align: middle;
  cursor: pointer;
  color: #fff;
}
table.list tr.rowodd td {
  padding: 0 5px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.roweven td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.list tr.none td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
.imgbtn {
  background-image: url(assets/imgs/add.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}
.imgico {
  cursor: pointer;
}
.searchwrapper {
  width:310px; /*follow your image's size*/
  height:40px;/*follow your image's size*/
  background-image:url(assets/imgs/searchbox.jpg);
  background-repeat:no-repeat; /*important*/
  padding:0px;
  margin:0px;
  position:relative; /*important*/
}
.searchbox {
  border:0px; /*important*/
  background-color:transparent; /*important*/
  position:absolute; /*important*/
  top:4px;
  left:9px;
  width:256px;
  height:31px;
  color:#fff;
  outline: none;
}
.searchbox_submit {
  border:0px; /*important*/
  background-color:transparent; /*important*/
  position:absolute; /*important*/
  top:4px;
  left:265px;
  width:32px;
  height:32px;
}
.styled-select {
  width: 240px;
  height: 34px;
  overflow: hidden;
  background: url(assets/imgs/down_arrow_select.jpg) no-repeat right #ddd;
  border: 1px solid #ccc;
  margin-left: 5px;
}
.styled-select select {
  background: transparent;
  width: 268px;
  padding: 5px;
  font-size: 16px;
  line-height: 1;
  border: 0;
  border-radius: 0;
  height: 34px;
  -webkit-appearance: none;
}
.styled-select option {
  padding: 5px;
  font-size: 16px;
  line-height: 1;
}
.drwrap {
  background: #ddd;
  border: 1px solid #666;
  height: 34px;
  margin-left: 5px;
  line-height: 32px;
  vertical-align: middle;
  font-family: Lucida Sans Unicode;
}
.drwrap input[type=text]{
  border: 1px solid #aaa;
  height: 23px;
  padding-left: 5px;
}
</style>
</head>
<body>
<div>
  <table cellPadding=0 cellSpacing=0>
    <tr>
      <td>
        <div class="searchwrapper">
          <input type="text" class="searchbox" id="s_kw" value="<?php echo $find; ?>" onkeypress="enter(event);"/>
          <input type="image" src="assets/imgs/blank.png" class="searchbox_submit" value="" onclick="find();"/>
        </div>
      </td>
      <td>
        <div class="drwrap">
          &nbsp;From&nbsp;<input id="s_from" type="text" value="<?php echo $from; ?>"></input>&nbsp;to&nbsp;<input id="s_to" type="text" value="<?php echo $to; ?>"></input>&nbsp;
        </div>
      </td>
      <td>
        <div class="styled-select">
          <select id="s_cat" onchange="find();">
            <option>All Users</option>
            <option>Inactivated Only</option>
            <option>Activated Only</option>
            <option>Suspended Only</option>
          </select>
        </div>
      </td>
    </tr>
  </table>
</div>
<div>&nbsp;</div>
<button class="imgbtn" onclick="parent.location.href='http://<?php echo URL . "admin.php?i=$page_id&script=index_customer"; ?>';">New Customer</button>
<div>
  <table class="list" style="margin-top: 5px;" cellPadding="0" cellSpacing="0">
    <tr>
      <th class="sort" onclick="set_order('account')">Account</th>
      <th class="sort" onclick="set_order('first_name')"width="100px">First Name</th>
      <th class="sort" onclick="set_order('last_name')"width="100px">Last Name</th>
      <th class="sort" onclick="set_order('phone')"width="100px">Phone</th>
      <th class="sort" onclick="set_order('is_actived')"width="100px">Status</th>
      <th class="sort" onclick="set_order('member_id')"width="100px">Membership</th>
      <th class="sort" onclick="set_order('reg_date')"width="60px">Date</th>
      <th class="sort" width="60px"></th>
    </tr>
    <?php foreach ($items->d as $idx => &$item) { ?>
    <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
      <td><?php echo MiscUtils::getShortName($item->account, 30); ?></td>
      <td><?php echo $item->first_name; ?></td>
      <td><?php echo $item->last_name; ?></td>
      <td><?php echo $item->phone; ?></td>
      <td>
        <select onchange="set_status(this, '<?php echo $item->id; ?>')" style="width: 98%">
        <?php 
          foreach ($status as &$tmp) {
            if ($tmp->id == $item->is_actived) {
              echo "<option value='$tmp->id' selected='selected'>$tmp->name</option>";
            } else {
              echo "<option value='$tmp->id'>$tmp->name</option>";
            }
          }
        ?>
      </select></td>
      <td>
        <select onchange="set_membership(this, '<?php echo $item->id; ?>')" style="width: 98%">
        <?php 
          foreach ($members as &$mem) {
          	if ($mem->id == $item->member_id) {
          	  echo "<option value='$mem->id' selected='selected'>$mem->title</option>";
          	} else {
              echo "<option value='$mem->id'>$mem->title</option>";
          	}
          }
        ?>
      </select></td>
      <td><?php echo $item->reg_date; ?></td>
      <td>
        <img src="assets/imgs/edit.png" class="imgico" onclick="parent.location.href='http://<?php echo URL . "admin.php?i=$page_id&script=index_customer&id=$item->id"; ?>';"></img>
<!--        <img src="assets/imgs/delete24.png" class="imgico" onclick="del_customer('<?php echo $item->id; ?>');"></img>-->
      </td>
    </tr>
    <?php } ?>
    <?php if (count($items->d) == 0) { ?>
    <tr class="none"><td colspan=8>No Results</td></tr>
    <?php } ?>
  </table>
  <div id="pagination"></div>
</div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../incls/js/pagination.js"></script>
<script src="../../../libs/jquery_ui/js/jquery-ui-1.10.2.custom.js"></script>
<link href="jquery_ui/css/smoothness/jquery-ui-1.10.2.custom.css" rel="stylesheet">
<script type="text/javascript">
  function set_order(col) {
    parent.location.href='http://<?php echo URL. "admin.php?i=$page_id&p=$items->p&q=" . (($items->q == 'ASC') ? 'DESC' : 'ASC'); ?>&o=' +  col + '&find=' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + encodeURIComponent($('#s_from')[0].value) + '&to=' + encodeURIComponent($('#s_to')[0].value);
  }

  function set_status(elm, id) {
    var active = elm.options[elm.selectedIndex].value;
    
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=set_active&q=" + encodeURIComponent(JSON.stringify({id: id, active: active})),
      success: function(msg){
        location.reload();
      }
    });
  }

  function set_membership(elm, id) {
    var member_id = elm.options[elm.selectedIndex].value;
    
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=set_membership&q=" + encodeURIComponent(JSON.stringify({id: id, member_id: member_id})),
      success: function(msg){
        location.reload();
      }
    });
  }
  
  function del_customer(id) {
    r = window.confirm('Are You Sure?');
    if (!r) {
      return;
    }
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=del_customer&q=" + encodeURIComponent(id),
      success: function(msg){
        location.reload();
      }
    });
  }
  
  function enter(ev) {
    if (window.event) {
      key = window.event.keyCode;
      isShift = window.event.shiftKey ? true : false;
    } else {
      key = ev.which;
    }
    if (key != 13) {
      return;
    }
    find();
  }
  
  function find() {
    var str = 'http://<?php echo URL . "admin.php?i=$page_id&find="; ?>' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + encodeURIComponent($('#s_from')[0].value) + '&to=' + encodeURIComponent($('#s_to')[0].value);
    parent.location.href = str;
  }

  $(function(){
    MyUtils.cleanDatepicker();
    $("#s_from").datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true,
      onSelect: function(dateText) {
        parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&find="; ?>' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + dateText + '&to=' + encodeURIComponent($('#s_to')[0].value);
      }
    });
    $("#s_to").datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true,
      onSelect: function(dateText) {
        parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&find="; ?>' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + encodeURIComponent($('#s_from')[0].value) + '&to=' + dateText;
      }
    });
    var elm = document.getElementById('pagination');
    MyUtils.removeChildElements(elm);
    
    $('#s_cat')[0].selectedIndex = <?php echo $cat; ?>;
    
    new ModulePagination(document, elm, null, null, null, null, {
      page: <?php echo $items->p; ?>,
      total: <?php echo $items->t; ?>,
      limit: <?php echo $items->s; ?>,
      callbackFunc: function(page) { 
        parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&o=$items->o&q=$items->q"; ?>&p=' + page + '&find=' + encodeURIComponent($('#s_kw')[0].value) + '&cat=' + $('#s_cat')[0].selectedIndex + '&from=' + encodeURIComponent($('#s_from')[0].value) + '&to=' + encodeURIComponent($('#s_to')[0].value);
      },
      contenxt: this
    });
  });
</script>
</body>
</html>